package org.example;

import java.util.Scanner;

public class TicTacToe {

    private char[][] board;
    private char currentPlayer;

    public TicTacToe() {
        board = new char[3][3];
        currentPlayer = 'X';
        initializeBoard();
    }

    // 初始化棋盘
    public void initializeBoard() {
        for (int i = 0; i < 3; i++){
            for (int j=0; j<3; j++){
                board[i][j]='-';
            }
        }
    }

    // 打印棋盘
    public void printBoard() {
        System.out.println("棋盘");
        for (int i=0;i<3;i++){
            for(int j=0; j<3;j++){
                System.out.print(board[i][j]+"");
            }
            System.out.println();
        }

    }

    // 判断棋盘是否满了
    public boolean isBoardFull() {
        for (int i =0;i <3;i++){
            for (int j=0;j<3;j++){
                if (board[i][j]=='-'){
                    return false;
                }
            }
        }
        return true;
    }
    // 更改玩家
    public void changePlayer() {
        currentPlayer =(currentPlayer =='X')?'0' : 'X';
    }

    // 放置标记
    public boolean placeMark(int row, int col) {
        if (row >=0 && col >=0 && row < 3 && col <3){
            if (board[row][col] =='-'){
                board[row][col] =currentPlayer;
                return true;
            }
        }
        return false;
    }

    // 判断是否有玩家获胜
    public boolean checkForWin() {
        for (int i=0;i<3;i++){
            if (board[i][0] ==currentPlayer && board[i][1] ==currentPlayer && board[i][2] == currentPlayer){
                return true;
            }
        }
        for (int i=0;i<3;i++){
            if (board[0][i] ==currentPlayer && board[1][i] ==currentPlayer && board[2][i] == currentPlayer) {
                return true;
            }
        }
        if (board[0][0] ==currentPlayer && board[1][1] ==currentPlayer && board[2][2] == currentPlayer) {
            return true;
        }
        if (board[0][2] ==currentPlayer && board[1][1] ==currentPlayer && board[2][0] == currentPlayer) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        TicTacToe game = new TicTacToe();
        game.printBoard();

        while (true) {
            int row, col;
            System.out.println("玩家 " + game.currentPlayer + "，请选择行（1、2、3）: ");
            row = scanner.nextInt() - 1;
            System.out.println("玩家 " + game.currentPlayer + "，请选择列（1、2、3）: ");
            col = scanner.nextInt() - 1;

            if (game.placeMark(row, col)) {
                game.printBoard();
                if (game.checkForWin()) {
                    System.out.println("玩家 " + game.currentPlayer + " 获胜！");
                    break;
                }
                if (game.isBoardFull()) {
                    System.out.println("游戏平局！");
                    break;
                }
                game.changePlayer();
            } else {
                System.out.println("输入的值不合法");
            }
        }
        scanner.close();
    }
}